Improve which-key-add-keymap-based-bindings
authorJustin Burkett <justin@burkett.cc>
Tue, 22 Jun 2021 11:37:04 +0000 (07:37 -0400)
committerJustin Burkett <justin@burkett.cc>
Tue, 22 Jun 2021 11:37:04 +0000 (07:37 -0400)
Add a test

which-key-tests.el
which-key.el

index 40566e73aa43700b07012b4f3a24210458b52ec8..877f0091964376e1b4a8d8182d38a3c3390eabdb 100644 (file)
     (define-key map "\C-b" prefix-map)
     (which-key-add-keymap-based-replacements map
       "C-a" '("mycomplete" . complete)
-      "C-b" "mymap")
+      "C-b" "mymap"
+      "C-c" "mymap2")
+    (define-key map "\C-ca" 'foo)
     (should (equal
              (which-key--get-keymap-bindings map)
              '(("C-a" . "mycomplete")
-               ("C-b" . "group:mymap"))))))
+               ("C-b" . "group:mymap")
+               ("C-c" . "group:mymap2"))))))
 
 (ert-deftest which-key-test--prefix-declaration ()
   "Test `which-key-declare-prefixes' and
index 2d81d2e2739a3675e7c1c8276590be8de768e852..2bfbb3975f37c3ce791df5285ae7c8d418083418 100644 (file)
@@ -922,8 +922,15 @@ actually bound to write-file before performing the replacement."
     (cond ((consp replacement)
            (define-key keymap (kbd key) replacement))
           ((stringp replacement)
-           (define-key keymap (kbd key) (cons replacement
-                                              (lookup-key keymap (kbd key)))))
+           (let ((binding (lookup-key keymap (kbd key))))
+             (if (or (null binding)
+                     (numberp binding))
+                 ;; using a keymap in case someone intends to make this a
+                 ;; prefix. If they want to bind something else, they will just
+                 ;; end up overriding the prefix map
+                 (define-key keymap (kbd key)
+                   (cons replacement (make-sparse-keymap)))
+               (define-key keymap (kbd key) (cons replacement binding)))))
           (t
            (user-error "replacement is neither a cons cell or a string")))
     (setq key (pop more)